package cn.foodProject.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;

import cn.foodProject.R;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.RegisterGsonBean;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "RegisterActivity";
    private Toolbar toolbarRegister;
    private EditText etRegName;
    private EditText etRegTelephone;
    private EditText etRegPassword;
    private EditText etRegPASSWORD;
    private Button btnRegister;
    private final Context context = RegisterActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        toolbarRegister = (Toolbar) findViewById(R.id.toolbar_register);
        etRegName = (EditText) findViewById(R.id.et_reg_name);
        etRegTelephone = (EditText) findViewById(R.id.et_reg_telephone);
        etRegPassword = (EditText) findViewById(R.id.et_reg_password);
        etRegPASSWORD = (EditText) findViewById(R.id.et_reg_PASSWORD);
        btnRegister = (Button) findViewById(R.id.btn_register);

        //toolbar相关
        setSupportActionBar(toolbarRegister);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbarRegister.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:

                //获取输入框内容并判断是否有空
                final String name = etRegName.getText().toString();
                final String phone = etRegTelephone.getText().toString();
                final String password = etRegPassword.getText().toString();
                String PASSWORD = etRegPASSWORD.getText().toString();
                boolean noEmpty = !name.isEmpty() && !phone.isEmpty() && !password.isEmpty() && !PASSWORD.isEmpty();

                //当没有空的输入时,判断两次输入的密码是否相同,相同才进行注册
                if (noEmpty) {
                    boolean canRegister = password.equals(PASSWORD);
                    if (canRegister) {
                        new Thread() {
                            @Override
                            public void run() {
                                super.run();
                                register(name, phone, password);
                            }
                        }.start();
                    } else {
                        Toast.makeText(context, "两次输入的密码不一致!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(context, "请不要留空!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 使用同步请求进行用户注册
     * @param name 用户名
     * @param telephone 手机号
     * @param password 密码
     */
    public void register(String name, String telephone, String password) {
        //构建json数据
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", name);
            jsonObject.put("telephone", telephone);
            jsonObject.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //构建url并获取call然后将请求加入队列
        URL url = MyUrl.getUrl("/user/register");
        try {
            Response response = MyOkhttp.getResponse(url, jsonObject.toString());
            if (response.isSuccessful()) {
                //使用Gson解析返回的json数据
                String string = response.body().string();
                final RegisterGsonBean registerGsonBean = (RegisterGsonBean) MyGson.fromJson(string, RegisterGsonBean.class);

                //如果请求成功,则返回登录页面
                if (registerGsonBean.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                    finish();
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, registerGsonBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            } else {
                Log.d(TAG, "onFailure: 请求失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
